from sc_engine.repositories.participant import ParticipantRepository
from sc_engine.scrape.service import get_player_info
from sc_engine.apps import messages as msgs

def make_app(app_name, root_config, config):
    lookup = get_player_info
    app = FetchPlayerApp(lookup)

    return {
        msgs.PlayerFetchRequested: app.player_fetch_requested,
    }

class FetchPlayerApp(object):
    """Fetch information on players from the kespa web site."""

    def __init__(self, lookup):
        self.lookup = lookup

    def player_fetch_requested(self, msg):
        result = self.lookup(msg.player_id)

        if result:
            return msgs.PlayerFetchAnnouncement(msg.player_id,
                    result['name'],
                    result['race'])
        else:
            return msgs.PlayerFetchNonExistantPlayer(msg.player_id)
            

